<?php

namespace App\Service\OpenShop\Message;


use App\Library\Rms\RmsShopApi;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Service\Common\CommonService;

class BaseMessageService
{
    protected ?OpenShopSignOrderModel $signOrder;
    protected object                  $shopInfo;
    protected string                  $orderNo;

    /**
     * @return OpenShopSignOrderModel|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getSignOrder()
    {
        return $this->signOrder;
    }

    /**
     * @param OpenShopSignOrderModel|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null $signOrder
     */
    public function setSignOrder($signOrder): void
    {
        $this->signOrder = $signOrder;
    }

    public function __construct(string $orderNo = '')
    {
        if (!empty($orderNo)) {
            $this->setSignOrder(OpenShopSignOrderModel::query()
                                                      ->where('orderNo', $orderNo)
                                                      ->first()
            );
            $this->orderNo = $orderNo;
        }
    }

    /**
     * 订单号
     *
     * @return string
     */
    public function getOrderNo(): string
    {
        return $this->orderNo;
    }

    /**
     * 门店详情
     *
     * @return object
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getShopInfo(): object
    {
        if (empty($this->shopInfo)) {
            $this->shopInfo = (object)(new RmsShopApi())->getShopByCode($this->getShopCode())['data'];
        }

        return $this->shopInfo;
    }

    /**
     * 门店编号
     *
     * @return int
     */
    public function getShopCode(): int
    {
        return $this->getSignOrder()->shopCode;
    }

    /**
     * 门店名称
     *
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getShopName()
    {
        return $this->getShopInfo()->name;
    }

    public function getShopUserName()
    {
        return CommonService::getUserInfoHide($this->getSignOrder()->identityCardRel->name ?? '', 'name');
    }

    public function getShopUserMobile()
    {
        return CommonService::getUserInfoHide($this->getSignOrder()->baseInfoRel->mobile ?? '', 'mobile');
    }

    public function getNowTime()
    {
        return date('Y-m-d H:i:s');
    }
}
